package action;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import model.CartModel;
import model.ProductCartDetail;
import model.ProductModel;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import pojo.Overview;
import pojo.Product;
import pojo.ProductImage;
import pojo.Specification;
import service.Tools;

import com.opensymphony.xwork2.ActionSupport;

import dao.ProductDAO;

public class DetailProductAction extends ActionSupport implements RequestAware, SessionAware {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7925339918470844556L;
	private Map<String, Object> request;
    private Map<String, Object> session;
    
    @Autowired
    ProductDAO ProductDAO;
    
    private String sku;
	public void setSku(String sku) {
		this.sku = sku;
	}

	private String action;

	public void setAction(String action) {
		this.action = action;
	}
	
	private double star;
	public void setStar(String star){
		double _star = Double.parseDouble(star);
		
		this.star = _star;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	
	public String execute() {
		
		// ===============================================================================================
		if (action != null && action.equals("AddToCart"))
		{
			Product product = ProductDAO.getProductBySKU(sku);
			if (product != null)
			{
				CartModel cartModel = (CartModel)session.get("cart");
				if (cartModel == null)
				{
					cartModel = new CartModel();
					cartModel.listProduct.add(new ProductCartDetail(product, 1));
					cartModel.price = cartModel.price.add(product.getPrice());
					cartModel.totalQuantity ++;
					session.put("cart", cartModel);
				}
				else
				{
					boolean flag = false;
					for ( int i = 0; i < cartModel.listProduct.size(); i++)
					{
						if ( cartModel.listProduct.get(i).productcart.getSku().equals(product.getSku()))
						{
							 ProductCartDetail pcd = cartModel.listProduct.get(i);
							 pcd.addQuantity(1);
							 cartModel.listProduct.set(i, pcd);
							cartModel.price = cartModel.price.add(product.getPrice());
							flag = true;
							break;
						}
					}
					if (flag == false)
					{
						cartModel.listProduct.add(new ProductCartDetail(product, 1));
						cartModel.price = cartModel.price.add(product.getPrice());
					}
					cartModel.totalQuantity ++;
					
				}
			}
		}
		else if (action != null && action.equals("AddToWishList"))
		{
			
		}
		else if(action != null && action.equals("rating")){
			Product product = ProductDAO.getProductBySKU(sku);
			if (product != null)
			{
				product.setRate(star);
			}
		}
		
		// ====================================================================================================
		
		Product product = ProductDAO.getProductBySKU(sku);
		
		ProductModel productmodel = new ProductModel(product.getSku(), product.getCategory(), product.getIdProduct(), product.getModel(), product.getName(), product.getPrice(), product.isAvailable());
		
		Iterator lstImage = product.getProductImages().iterator();
		while (lstImage.hasNext() && productmodel.productImages.size() < 5)
		{
			productmodel.productImages.add( (ProductImage)lstImage.next());
		}
		if (product.getOverviews().iterator().hasNext() )
		{
			Overview ov = (Overview)product.getOverviews().iterator().next();
			productmodel.features = Tools.getInstance().parseString(ov.getProductFeatures(), '~');
			productmodel.includes = Tools.getInstance().parseString(ov.getIncludeItem(), '~');
			productmodel.description = ((Overview)product.getOverviews().iterator().next()).getDescription();
		}
		productmodel.accessories = product.getAccessories();
		productmodel.rateReviews = product.getRateReviews();
		productmodel.rate = product.getRate();
		
		if (product.getSpecifications().iterator().hasNext())
			productmodel.specification = (Specification)product.getSpecifications().iterator().next();
		
		productmodel.deviceBrand = product.getDeviceBrand();
		productmodel.operatingSystem = product.getOperatingSystem();
		
		request.put("product", productmodel);
        return SUCCESS;
    }
}
